<?php
	require_once("../inc/function.php");
	
function GetOrderDetails()
{
	$conn = GetConnection();
    return $conn->query("SELECT * FROM  `OrderDetails` ");
	
}
function NewOrderDetail($row)
{
	$order = $row['order_id'];
	$product = $row['product_id'];
	$name = $row['name'];
	$price = $row['price'];	
	$SKU = rand_string(20);		
	$quantity = $row['quantity'];
	$customer = $row['customer_id'];
	$card = $row['type'];	
	$number = $row['number'];	
	$exp = $row['exp'];
	$conn = GetConnection();
	$conn->query("INSERT INTO `OrderDetails` Values(NULL, '$order', '$product', '$name', '$price', '$SKU', '$quantity','$customer', '$card', '$number', '$exp')");
	$conn->close();		
}
function GetOrderDetail($id)
{
	$conn = GetConnection();
    $sql = "SELECT * FROM `OrderDetails` WHERE id=$id";
    $results = $conn->query($sql);
    //echo $sql;
    $row = $results->fetch_assoc();
    $conn->close();
    return $row;	
}
function SaveOrderDetail($row)
{
	$id = $row['id'];
	$order = $row['order_id'];
	$product = $row['product_id'];
	$name = $row['name'];
	$price = $row['price'];		
	$quantity = $row['quantity'];
	$customer = $row['customer_id'];
	$card = $row['type'];	
	$number = $row['number'];	
	$exp = $row['exp'];
	$conn = GetConnection();
	if(strlen($name)>0)
	{
		   $conn->query("UPDATE `OrderDetails` "
			.	"SET Order_id='$order', Product_id ='$product', NameOfOrder ='$name', price ='$price', quantity='$quantity', Customer_id='$customer', Card_Type ='$card', Card_Number='$number', ExpDate ='$exp' "
			.	"WHERE id='$id' ");        
	}
	else {
		header("Location: ../Orders/edit.php");
	}		  
       $conn->close();	
       return $error == '' ? true : array('SQL error' => $error);
	$conn->close();	
}
function DeleteOrderDetail($id)
{
	$conn = GetConnection();
	$results = $conn->query("DELETE FROM OrderDetails WHERE id=$id");
	header('Location: http://cs.newpaltz.edu/~N01852322/Final/OrderDetails/');
}

function GetCustomers()
{
        $conn = GetConnection();
        return $conn->query('SELECT * FROM Customer');
}

function GetOrders()
{
	$conn = GetConnection();
    return $conn->query("SELECT * FROM  `Order` ");   	
}

function GetProducts()
{
	$conn = GetConnection();
    return $conn->query("SELECT * FROM  `Product` ");
}
function rand_string( $length ) 
{
	$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";	

	$size = strlen( $chars );
	for( $i = 0; $i < $length; $i++ ) 
	{
		$str .= $chars[ rand( 0, $size - 1 ) ];
	}
	return $str;
}
?>